package com.ht.medicaltreatment.view

import android.content.Context
import android.view.View
import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.ToastUtils
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.api.RetrofitUtils
import com.ht.medicaltreatment.bean.IceboxItemListBean
import com.ht.medicaltreatment.bean.ProjectBean
import com.xgst.driverlesscars.api.ApiService
import com.xgst.lib.http.ApiResult
import kotlinx.android.synthetic.main.dialog_project_layout.*
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch

//样本类型
class TypeDialog(context: Context):BaseDialog(context,300f) {
    override fun getLayoutRes() = R.layout.dialog_type_layout

    private var itemDialog = ItemListDialog(context)
    private var mId = -1

    fun show(onSave:(String,String)->Unit) {
        super.show()
        imgBG.setOnClickListener {
            llProgress.visibility = View.VISIBLE
            projects({
                val list = mutableListOf<IceboxItemListBean>()
                it.forEach { bean->
                    list.add(IceboxItemListBean(bean.id,bean.name))
                }
                llProgress.visibility = View.GONE
                itemDialog.show(list,{name,id ->
                    tvProjectName.text = name
                    mId = id
                })
            })
        }
        mbNegative.setOnClickListener {
            dismiss()
        }
        mbPositive.setOnClickListener {
            if(mId == -1)return@setOnClickListener
            onSave("$mId",tvProjectName.text.toString())
            dismiss()
        }
    }

    //项目
    private fun projects(onResult:(MutableList<ProjectBean>)->Unit){
        MainScope().launch {
            val bean = RetrofitUtils.initRetrofit(ApiService::class.java).type()
            when(bean){
                is ApiResult.Success -> onResult(bean.data.data!!)
                is ApiResult.Error ->  llProgress.visibility = View.GONE
            }
        }
    }
}